package com.school.graduation.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;


public class UserUtil {
    public static boolean hasLogIn = false;
    private static SharedPreferences sp;
    private static UserUtil userUtil;
    private String name;
    private String id;
    private String city;
    private String password;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public static UserUtil getCurrentUser(Context context){
        sp = context.getApplicationContext().getSharedPreferences("user_cache", Context.MODE_MULTI_PROCESS |
                Activity.MODE_WORLD_READABLE);
        String mName = "no_name_flag";
        try {
            mName = sp.getString("name","no_name_flag");
        }catch (NullPointerException e){
            return null;
        }
        if (mName.equals("no_name_flag")) return null;
        if (userUtil==null) {
            userUtil = new UserUtil();
            userUtil.setName(sp.getString("name","no_name_flag"));
            userUtil.setCity(sp.getString("city","no_city_flag"));
            userUtil.setPassword(sp.getString("password","no_password_flag"));
            userUtil.setId(sp.getString("id","no_id_flag"));
        }
        return userUtil;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public static void logOut(){
        SharedPreferences.Editor editor = sp.edit();
        /*editor.putString("name","no_name_flag");
        editor.putString("city","no_city_flag");
        editor.putString("id","no_id_flag");
        editor.putString("password","no_password_flag");*/
        editor.clear();
        editor.apply();
    }
}
